﻿using System.Web.Mvc;
using System.Text;
using System.Collections;
using BPMS.IBusiness;
using BPMS.Business;
using System.Data;
using BPMS.Entity;
using Newtonsoft.Json;
using System.Collections.Generic;
using System;
using DotNet.Utilities;
using System.Web;
namespace BPMS.MVC.Controllers
{
    public class JsonController : Controller
    {
        #region 读取Json
        /// <summary>
        /// 读取json
        /// </summary>
        /// <param name="validate"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="SubCompany"></param>
        /// <param name="Company"></param>
        /// <param name="Department"></param>
        /// <param name="Workgroup"></param>
        /// <returns></returns>
        public ActionResult Index(string validate = null, int PageIndex = -1, int PageSize = -1, string SubCompany = null, string Company = null, string Department = null, string Workgroup = null)
        {
            BPMS_PersonIBLL bpms_personibll = new BPMS_PersonBLL();
            StringBuilder strWhere = new StringBuilder();
            string jsonStr = null;
            int count = 0;
            Hashtable param = new Hashtable();
            param["Validate"] = validate;
            param["SubCompany"] = SubCompany;
            param["Company"] = Company;
            param["Department"] = Department;
            param["Workgroup"] = Workgroup;
            DataTable dt = bpms_personibll.GetPageList2(param, "SubCompanyId", "desc", PageIndex, PageSize, ref count);
            Root r = new Root();
            DataView dv = dt.DefaultView;
            dv.Sort = "SubCompanyId,CompanyId,DepartmentId,WorkgroupId";
            dt = dv.ToTable();
            r = GetTree(SubCompany, Company, Department, Workgroup);
            r.totalNum = count;
            int[] flag = new int[4] { 0, 0, 0, 0 };
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < r.SubCompanys.Count; i++)
                    if (dr["SubCompanyId"].ToString() == r.SubCompanys[i].RealName)
                    {
                        for (int j = 0; j < r.SubCompanys[i].Companys.Count; j++)
                            if (dr["CompanyId"].ToString() == r.SubCompanys[i].Companys[j].RealName)
                            {
                                for (int k = 0; k < r.SubCompanys[i].Companys[j].Departments.Count; k++)
                                    if (dr["DepartmentId"].ToString() == r.SubCompanys[i].Companys[j].Departments[k].RealName)
                                    {
                                        for (int l = 0; l < r.SubCompanys[i].Companys[j].Departments[k].WorkGroups.Count; l++)
                                            if (dr["WorkgroupId"].ToString() == r.SubCompanys[i].Companys[j].Departments[k].WorkGroups[l].RealName)
                                                r.SubCompanys[i].Companys[j].Departments[k].WorkGroups[l].Persons.Add(RowToPerson(dr));
                                            else if (dr["WorkgroupId"].ToString() == "")
                                            {
                                                r.SubCompanys[i].Companys[j].Departments[k].WorkGroups.Find(t => t.RealName == "未分组").Persons.Add(RowToPerson(dr));
                                                break;
                                            }
                                    }
                                    else if (dr["DepartmentId"].ToString() == "")
                                    {
                                        r.SubCompanys[i].Companys[j].Departments.Find(t => t.RealName == "未分组").WorkGroups.Find(t => t.RealName == "未分组").Persons.Add(RowToPerson(dr));
                                        break;
                                    }
                            }
                            else if (dr["CompanyId"].ToString() == "")
                            {
                                r.SubCompanys[i].Companys.Find(t => t.RealName == "未分组").Departments.Find(t => t.RealName == "未分组").WorkGroups.Find(t => t.RealName == "未分组").Persons.Add(RowToPerson(dr));
                                break;
                            }
                    }
            }

            //for (int i = 0; i < r.SubCompanys.Count; i++)
            //{
            //    for (int j = 0; j < r.SubCompanys[i].Companys.Count; j++)
            //    {
            //        if (r.SubCompanys[i].Companys[j].RealName == "未分组" && r.SubCompanys[i].Companys[j].Departments.Count == 0)
            //        {
            //            r.SubCompanys[i].Companys.Remove(r.SubCompanys[i].Companys[j]);
            //            continue;
            //        }
            //        for (int k = 0; k < r.SubCompanys[i].Companys[j].Departments.Count; k++)
            //        {
            //            if (r.SubCompanys[i].Companys[j].Departments[k].RealName == "未分组" && r.SubCompanys[i].Companys[j].Departments[k].WorkGroups.Count == 0)
            //            {
            //                r.SubCompanys[i].Companys[j].Departments.Remove(r.SubCompanys[i].Companys[j].Departments[k]);
            //                continue;
            //            }
            //            for (int l = 0; l < r.SubCompanys[i].Companys[j].Departments[k].WorkGroups.Count; l++)
            //            {
            //                if (r.SubCompanys[i].Companys[j].Departments[k].WorkGroups[l].RealName == "未分组" && r.SubCompanys[i].Companys[j].Departments[k].WorkGroups[l].Persons.Count == 0)
            //                    r.SubCompanys[i].Companys[j].Departments[k].WorkGroups.Remove(r.SubCompanys[i].Companys[j].Departments[k].WorkGroups[l]);
            //            }
            //        }
            //    }
            //}

            for (int i = 0; i < r.SubCompanys.Count; i++)
            {
                for (int j = 0; j < r.SubCompanys[i].Companys.Count; )
                {
                    for (int k = 0; k < r.SubCompanys[i].Companys[j].Departments.Count; )
                    {
                        for (int l = 0; l < r.SubCompanys[i].Companys[j].Departments[k].WorkGroups.Count; )
                        {
                            if (r.SubCompanys[i].Companys[j].Departments[k].WorkGroups[l].Persons.Count == 0)
                            {
                                r.SubCompanys[i].Companys[j].Departments[k].WorkGroups.Remove(r.SubCompanys[i].Companys[j].Departments[k].WorkGroups[l]);
                                l = 0;
                            }
                            else
                                l++;
                        }
                        if (r.SubCompanys[i].Companys[j].Departments[k].WorkGroups.Count == 0)
                        {
                            r.SubCompanys[i].Companys[j].Departments.Remove(r.SubCompanys[i].Companys[j].Departments[k]);
                            k = 0;
                        }
                        else
                            k++;
                    }
                    if (r.SubCompanys[i].Companys[j].Departments.Count == 0)
                    {
                        r.SubCompanys[i].Companys.Remove(r.SubCompanys[i].Companys[j]);
                        j = 0;
                    }
                    else
                        j++;
                }
                //if (r.SubCompanys[i].Companys.Count == 0)
                //{
                //    r.SubCompanys.Remove(r.SubCompanys[i]);
                //    i = 0;
                //}
                //else
                //    i++;
            }
            jsonStr = JsonConvert.SerializeObject(r, Formatting.Indented);
            ViewBag.json = jsonStr;
            return View();
        }
        #endregion

        #region 获得组织列表
        /// <summary>
        /// 获得组织列表
        /// </summary>
        /// <param name="Sub"></param>
        /// <param name="Com"></param>
        /// <param name="Dep"></param>
        /// <param name="Wor"></param>
        /// <returns></returns>
        public Root GetTree(string Sub = null, string Com = null, string Dep = null, string Wor = null)
        {

            BPMS_OrganizationIBLL bpms_organizationibll = new BPMS_OrganizationBLL();
            Root r = new Root();
            SubCompany s;
            IList list = bpms_organizationibll.GetList();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == "0");
            List<BPMS_Organization> itemNodeSel = itemNode.FindAll(t => t.FullName == Sub);
            if (Sub == null)
                itemNodeSel = itemNode;
            foreach (BPMS_Organization entity in itemNodeSel)
            {
                s = new SubCompany();
                s.RealName = entity.FullName;
                s.Companys = GetTreeNode1(entity.OrganizationId, list, Com, Dep, Wor);
                r.SubCompanys.Add(s);
            }
            return r;
        }
        /// <summary>
        /// 递归1
        /// </summary>
        /// <param name="ParentId"></param>
        /// <param name="list"></param>
        /// <param name="Com"></param>
        /// <param name="Dep"></param>
        /// <param name="Wor"></param>
        /// <returns></returns>
        public List<Company> GetTreeNode1(string ParentId, IList list, string Com = null, string Dep = null, string Wor = null)
        {
            List<Company> cList = new List<Company>();
            Company c;
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == ParentId);
            List<BPMS_Organization> itemNodeSel = itemNode.FindAll(t => t.FullName == Com);
            if (Com == null)
                itemNodeSel = itemNode;
            if (itemNode.Count > 0)
            {
                foreach (BPMS_Organization entity in itemNodeSel)
                {
                    c = new Company();
                    c.RealName = entity.FullName;
                    //创建子节点
                    c.Departments = GetTreeNode2(entity.OrganizationId, list, Dep, Wor);
                    cList.Add(c);
                }
            }
            c = new Company();
            c.RealName = "未分组";
            cList.Add(c);
            return cList;
        }
        /// <summary>
        /// 递归2
        /// </summary>
        /// <param name="ParentId"></param>
        /// <param name="list"></param>
        /// <param name="Dep"></param>
        /// <param name="Wor"></param>
        /// <returns></returns>
        public List<Department> GetTreeNode2(string ParentId, IList list, string Dep = null, string Wor = null)
        {
            List<Department> dList = new List<Department>();
            Department d;
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == ParentId);
            List<BPMS_Organization> itemNodeSel = itemNode.FindAll(t => t.FullName == Dep);
            if (Dep == null)
                itemNodeSel = itemNode;
            if (itemNode.Count > 0)
            {
                foreach (BPMS_Organization entity in itemNodeSel)
                {
                    d = new Department();
                    d.RealName = entity.FullName;
                    //创建子节点
                    d.WorkGroups = GetTreeNode3(entity.OrganizationId, list, Wor);
                    dList.Add(d);
                }
            }
            d = new Department();
            d.RealName = "未分组";
            dList.Add(d);
            return dList;
        }
        /// <summary>
        /// 递归3
        /// </summary>
        /// <param name="ParentId"></param>
        /// <param name="list"></param>
        /// <param name="Wor"></param>
        /// <returns></returns>
        public List<WorkGroup> GetTreeNode3(string ParentId, IList list, string Wor = null)
        {
            List<WorkGroup> wgList = new List<WorkGroup>();
            WorkGroup w;
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == ParentId);
            List<BPMS_Organization> itemNodeSel = itemNode.FindAll(t => t.FullName == Wor);
            if (Wor == null)
                itemNodeSel = itemNode;
            if (itemNode.Count > 0)
            {
                foreach (BPMS_Organization entity in itemNodeSel)
                {
                    w = new WorkGroup();
                    w.RealName = entity.FullName;
                    wgList.Add(w);
                }
            }
            w = new WorkGroup();
            w.RealName = "未分组";
            wgList.Add(w);
            return wgList;
        }
        #endregion

        #region DataRow转成Person对象
        /// <summary>
        /// DataRow转成Person对象
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private static Person RowToPerson(DataRow dr)
        {
            Person per = new Person();
            per.PerId = dr["PerId"].ToString();
            per.RealName = dr["RealName"].ToString();
            per.SubCompany = dr["SubCompanyId"].ToString();
            per.Company = dr["CompanyId"].ToString();
            per.Department = dr["DepartmentId"].ToString();
            per.Workgroup = dr["WorkgroupId"].ToString();
            per.Tel1 = dr["Tel1"].ToString();
            per.Tel2 = dr["Tel2"].ToString();
            per.Tel3 = dr["Tel3"].ToString();
            per.ModifyDate = (DateTime?)dr["ModifyDate"];
            return per;
        }
        #endregion
    }
}
